class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        already_dict = {}
        for i in range(len(nums)):
            if nums[i] in already_dict:
                if i - already_dict[nums[i]] <= k:
                    return True
                else:
                    already_dict[nums[i]] = i
            else:
                already_dict[nums[i]] = i
        return False


data = Solution()
nums = [1,2,3,1,2,3]
k = 2
print(data.containsNearbyDuplicate(nums, k))
nums = [1,0,1,1]
k = 1
print(data.containsNearbyDuplicate(nums, k))
nums = [1,2,3,1]
k = 3
print(data.containsNearbyDuplicate(nums, k))